<?php

namespace app\admin\model;

use think\Model;


class Shoptableorder extends Model
{

    // 表名
    protected $name = 'shop_table_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'status_text',
        'update_time_text'
    ];



    public function formatStatus($status){

        if($status == 'paying'){
            return "待付款";
        }else if($status == "payed"){
            return "待消费";
        }else if($status == 'doing'){
            return "消费中";
        }else if($status == "done"){
            return "已完成";
        }else if($status == "refund"){
            return "已退款";
        }
    }

    public function user(){

        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);

    }

    public function getStatusList()
    {
        return ['payed' => __('未开始'), 'doing' => __('进行中'), 'done' => __('已完成'), 'refund' => __('退款')];
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
